<template>
  <view>
    <view class="address-container-box">
      <view class="address-container"  v-if="JSON.stringify(address) === '{}'">
        <!-- 没有地址选择时的按钮展示 -->
        <!-- 根据address里面有没有内容来选择显示那一个 -->
          <button type="primary" size="mini" class="add-address" @click="getAddress">添加收货地址+</button>
      </view>
      <!-- 选择的地址信息展示 -->
      <view class="current-address-box" v-else @click="getAddress">
        <view class="row1">
          <view class="row1-left">
            收货人姓名：{{address.userName}}
          </view>
          <view class="row1-right">
            <text class="phone">电话好码：{{address.telNumber}}</text>
            <uni-icons type="arrowright" size="12px"></uni-icons>
          </view>
        </view>
        <view class="row2">
            收货地址：{{addStr}}
        </view>
      </view>
    </view>
    <image src="../../static/cart_border@2x.png" class="address-pic"></image>
  </view>
</template>

<script>
  import { mapState, mapGetters, mapMutations } from 'vuex'
  
  export default {
    name: "my-address",
    data() {
      return {
        // address: {}
      };
    },
    computed: {
      ...mapState('address', ['address']),
      ...mapGetters('address', ['addStr']),
    },
    methods: {
      ...mapMutations('address', ['updateAddress']),
      async getAddress() {
        // 调用微信的API
        const [err, succ] = await uni.chooseAddress().catch(error => error)
        // 判断是点击了确认还是取消
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          // 选择了地址
          // this.address = succ
          this.updateAddress(succ)
        }
      }
    }
  }
</script>

<style lang="scss">
  .address-container-box {
    font-size: 12px;
    height: 90px;
    padding: 0 10px;

    .address-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
    }


    .current-address-box {
      display:  flex;
      flex-direction: column;
      justify-content: center;
      height: 100%;
      padding: 0 5px;

      .row1 {
        display: flex;
        justify-content: space-between;

        .row1-left {}

        .row1-right {
          .phone {
            margin-right: 10px;
          }
        }
      }

      .row2 {
        margin-top: 10px;
        white-space: nowrap;
        overflow: hidden;
      }
    }
  }

  .address-pic {
    display: block;
    width: 100%;
    height: 5px;
  }
</style>
